package com.gfxy.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gfxy.modules.common.entity.MemberList;
import com.gfxy.modules.common.entity.TotalScore;
import com.gfxy.modules.common.mapper.ScoreUpdateMapper;
import com.gfxy.modules.common.service.IMemberListService;
import com.gfxy.modules.common.service.ITotalScoreService;
import com.gfxy.modules.common.service.ScoreUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Author: JinChongLi
 * @Description:
 * @Date: Created in 16:32 2021/11/22
 * @Modified By:
 */
@Component
public class ScoreUpdateImpl implements ScoreUpdate {

    @Autowired
    ITotalScoreService totalScoreService;
    @Autowired
    IMemberListService memberListService;

    @Autowired
    ScoreUpdateMapper scoreUpdateMapper;

    @Override
    public void updataNewScore(MemberList memberList) {
        String teacherId = memberList.getTeacherId();
        String date = memberList.getDate();

        int items_value = memberList.getItemValue();

        List<Map> scores = scoreUpdateMapper.getTotalScoreByItemValueAndTeacherIdAndDate(date,teacherId, items_value);

        for (Map score : scores) {
            QueryWrapper<TotalScore> eq1 = new QueryWrapper<TotalScore>()
                    .eq("date", memberList.getDate())
                    .eq("teacher_id", memberList.getTeacherId())
                    .eq("classification_id",score.get("classification_id"))
                    .eq("project_id",score.get("project_id"));
            totalScoreService.remove(eq1);

            TotalScore totalScore = new TotalScore();
            totalScore.setScore(Double.parseDouble(score.get("score").toString()));
            totalScore.setClassificationId(score.get("classification_id").toString());
            totalScore.setProjectId(Integer.parseInt(score.get("project_id").toString()));
            totalScore.setOrgCode(memberList.getOrgId());
            totalScore.setTeacherId(memberList.getTeacherId());
            totalScore.setDate(memberList.getDate());
            totalScoreService.save(totalScore);
        }

    }

}

